﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SWEN_HMS.Classes;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class ViewRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    if (Request.QueryString["updateSuccessful"] == "no")
                    {
                        viewReqLabel.Text = "Request ID does not exist!";
                    }
                    ShowGrid();
                }
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            Response.Cache.SetValidUntilExpires(false);
            Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void ShowGrid()
        {
            DataTable resultSet = StaffHousekeepingDBManager.getRequests();
            if (resultSet.Rows.Count != 0)
            {
                viewReqGridView.DataSource = resultSet;
                viewReqGridView.DataBind();
            }
            else
            {
                viewReqLabel.Text = "No requests available!";
            }
            reqCountLabel.Text = Convert.ToString(resultSet.Rows.Count);
        }

        protected void DeleteRequest(object sender, GridViewDeleteEventArgs ex)
        {
            int requestID = Convert.ToInt32(viewReqGridView.DataKeys[ex.RowIndex].Value.ToString());
            bool deleteRequest = StaffHousekeepingDBManager.deleteRequest(requestID);
            if (deleteRequest == true)
            {
                viewReqLabel.Text = "Request ID <b><u>" + Convert.ToString(requestID) + "</u></b> has been deleted!";
                ShowGrid();
            }
            else
            {
                viewReqLabel.Text = "Request ID <b><u>" + Convert.ToString(requestID) + "</u></b> has not been deleted!";
            }
        }

        protected void submitRequestBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/AddRequest.aspx");
        }
    }
}